//
//  MQStatusViewModel.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/20.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class MQStatusViewModel: NSObject {
    var status:MQStatus?{
        didSet{
            if let mbrank = status?.user?.mbrank where  mbrank > 0 && mbrank < 7 {
                memberImage = UIImage(named: "common_icon_membership_level\(mbrank)")
            }else{
                memberImage = nil
            }
            
            verifiedViewImage = calcVerifiedViewImage()
            
            repostsCount = calcStatusCount((status?.reposts_count)!, defaultTitle: "转发")
            commentsCount = calcStatusCount((status?.comments_count)!, defaultTitle: "评论")
            attitudesCount = calcStatusCount((status?.attitudes_count)!, defaultTitle: "赞")
        
            //判断是否有转发微博
            if let reweetStatus = status?.retweeted_status?.text {
                let name = status?.retweeted_status?.user?.screen_name
                retweetStatusText = "@\(name):\(reweetStatus)"
            }
            
            if let urls = status?.pic_urls where urls.count > 0{
                picUrls = urls
            }else if let urls = status?.retweeted_status?.pic_urls where urls.count > 0{
                picUrls = urls
            }
            dealSource()
            dealStatus(status?.text)
        }
    }
    
    var picUrls:[MQStatusPictureInfo]?

    var memberImage:UIImage?
    var verifiedViewImage:UIImage?
    var repostsCount:String?
    var commentsCount:String?
    var attitudesCount:String?
    var retweetStatusText:String?
    var sourceText:String?
    var createText:String?{
        return NSDate.sinaData((status?.created_at)!)?.sinaDateDescription
    }
    
    //处理来源字符串
    private func dealStatus(text:String?){
        guard let contentText = text else{
            return
        }
//        printfLog(text)
        //正则表达式搜索[xxxx]
        (contentText as NSString).enumerateStringsMatchedByRegex("\\[[a-zA-Z\\u4e00-\\u9fa5]+\\]") { (count, captureString, captureRange, _) -> Void in
//            printfLog(captureString.memory)
//            printfLog(captureRange.memory)
            
            if let emoticon = MQEmoticonTools.shareTools.emoticonWithChs(captureString.memory! as String){
                printfLog(emoticon.chs)
            }
            
            
        }
    }
    private func dealSource(){
        guard let text = status?.source else {
            return
        }
        
        if let preRange = text.rangeOfString("\">"){
            if let sufRang = text.rangeOfString("</"){
                sourceText = "来自\(text.substringWithRange(preRange.endIndex..<sufRang.startIndex))"
            }
        }
    }
    
    private func calcVerifiedViewImage() -> UIImage?{
        
            if let verified = status?.user?.verified_type{
                switch(verified){
                case -1:
                    return nil
                case 0:
                    return UIImage(named: "avatar_vip")
                case 2,3,5:
                    return UIImage(named: "avatar_enterprise_vip")
                case 220:
                    return UIImage(named: "avatar_grassroot")
                default:
                    return nil
            }
        }
        return nil
    }
    
    private func calcStatusCount(count:Int, defaultTitle:String) -> String{
        if count == 0{
            return defaultTitle
        }
        if count < 10000  {
            return "\(count)"
        }else{
            let result = CGFloat(count/1000)/10
            let resultStr = "\(result)"
            if resultStr.containsString(".0"){
                return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return resultStr
        }
    }
    
    override var description:String{
        let keys = ["status"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
  
}
